package com.ruoyi.project.socket.packet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class L2Packet {

    private L2Header l2Header;

    private L2Payload l2Payload;

    private int len;

    public L2Packet(L2Header l2Header, L2Payload l2Payload) {
        if(null==l2Header||null==l2Payload){
            throw new RuntimeException("l2Header或者l2Payload为空");
        }
        this.l2Header = l2Header;
        this.l2Payload = l2Payload;
        len+=l2Header.getLen();
        len+=l2Payload.getLen();
    }

    public int getLen(){
        return len;
    }

    public byte[] l2Packet2Bytes(){
        ByteBuf buf = Unpooled.buffer(l2Header.getLen()+l2Payload.getLen());
        buf.writeBytes(l2Header.l2Header2Bytes());
        byte[] l2PayloadBytes = l2Payload.l2Payload2Bytes();
        if(null==l2PayloadBytes)
            return null;
        buf.writeBytes(l2PayloadBytes);
        return buf.array();
    }

    public L2Header getL2Header() {
        return l2Header;
    }

    public void setL2Header(L2Header l2Header) {
        this.l2Header = l2Header;
    }

    public L2Payload getL2Payload() {
        return l2Payload;
    }

    public void setL2Payload(L2Payload l2Payload) {
        this.l2Payload = l2Payload;
    }
}
